home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
cool
/
ge_cool.lha
/
GE_COOL2.1
/
src
/
Range
/
Range.h
< prev
Wrap
C/C++ Source or Header
|
1992-05-18
|
6KB
|
153 lines
//
// Copyright (C) 1991 Texas Instruments Incorporated.
//
// Permission is granted to any individual or institution to use, copy, modify,
// and distribute this software, provided that this complete copyright and
// permission notice is maintained, intact, in all copies and supporting
// documentation.
//
// Texas Instruments Incorporated provides this software "as is" without
// express or implied warranty.
//
//
// Created: MBN 08/21/89 -- Initial design and implementation
// Updated: LGO 10/05/89 -- Make destructor inline
// Updated: LGO 12/04/89 -- Efficiency re-write
// Updated: DLS 03/27/91 -- New lite version
//
// The parameterized Range<Type,lbound,hbound> class is publicly derived from
// the Range class and supports arbitrary user-defined ranges for a type of
// object or built-in data type. This allows other higher level data structures
// such as the Vector and List container classes to be parameterized over a
// range of values for some type so that the programmer does not have to add
// bounds checking code to the application. A Vector of positive integers, for
// example, would be easy to declare, facilitating bounds checking restricted
// to the code that implements the type, not the Vector.
//
// The inclusive upper and lower bounds for the range are specified as
// arguments to the parameterized type declaration and implementation macro
// calls. They are declared as C++ constants of the appropriate type. No
// storage is allocated and all references are compiled out by the compiler.
// Once declared, a Range<Type,lbound,hbound> object cannot have its upper or
// lower bounds changed, since maintenance of all instances would require
// significant and unwarranted overhead. Each Range<Type,lbound,hbound> object
// has a single private data slot that holds ths instance value. There are
// three constructors. The first is a simple empty constructor. The second
// accepts and sets an initial value. The third takes a reference to another
// Range<Type,lbound,hbound> object and duplicates its value.
//
// All Range<Type,lbound,hbound> methods are implemented as small inline
// functions to provide efficient encapsulation of objects, including built-in
// types such as int. Methods are provided to get the lower and upper bounds
// and set the value of the instance data slot. Assignment of a value or one
// object to another is supported by the overloaded operator= method. Finally,
// an implicit conversion from a Range<Type,lbound,hbound> object to a Type
// value is provided to allow mixed expressions.
//
#ifndef RANGEH // If no Range class
#define RANGEH
#ifndef BASE_RANGEH // If no class definition
#include <cool/Base_Range.h> // Include header file
#endif
// ensure no multiple definitions
template <class Type, Type lbound, Type hbound> CoolRange {
typedef int (*Type##_CoolRange_Compare) (const Type&, const Type&);
}
template <class Type, Type lbound, Type hbound>
class CoolRange<Type,lbound,hbound> : public CoolRange {
public:
CoolRange<Type,lbound,hbound> () {}; // Simple constructor
CoolRange<Type,lbound,hbound> (const Type& value); // Constructor with value
CoolRange<Type,lbound,hbound> (const CoolRange<Type,lbound,hbound>&); // Copy
~CoolRange<Type,lbound,hbound> () {}; // Destructor
inline const Type& low () const; // Get low value range
inline const Type& high () const; // Get high value range
inline void set (const Type&); // Set instance value
inline CoolRange<Type,lbound,hbound>&operator=(const CoolRange<Type,lbound,hbound>&);
inline operator Type () const; // Implicit type conversion
inline void set_compare(Type##_CoolRange_Compare);// Set Range compare function
private:
Type data; // Storage for instance value
static Type low_bound; // Class lower bound
static Type high_bound; // Class upper bound
static Type##_CoolRange_Compare compare_s; // Pointer operator== function
char* value_string() const; // convert data to string
void report_set_error () const; // set method helper
void do_compare () const; // set method helper
friend int Type##_CoolRange_compare_data (const Type&, const Type&);
};
// Initialize lower/upper bounds
template <class Type, Type lbound, Type hbound> CoolRange {
Type CoolRange<Type,lbound,hbound>::low_bound = lbound;
Type CoolRange<Type,lbound,hbound>::high_bound = hbound;
}
// low -- Return a reference to lower limit to allow user to set/get value
// Input: None
// Output: Reference to lower limit
template <class Type, Type lbound, Type hbound>
inline const Type& CoolRange<Type,lbound,hbound>::low () const {
return this->low_bound;
}
// high -- Return a reference to upper limit to allow user to set/get value
// Input: None
// Output: Reference to upper limit
template <class Type, Type lbound, Type hbound>
inline const Type& CoolRange<Type,lbound,hbound>::high () const {
return this->high_bound;
}
// set -- Set the value of the object
// Input: Value to set
// Output: None
template <class Type, Type lbound, Type hbound>
inline void CoolRange<Type,lbound,hbound>::set (const Type& value) {
this->data = value;
if (this->compare_s != NULL) // When Custom compare function
this->do_compare();
else // Normal, fast case
// check both bounds at once to minimize the amount of inline code
if (value < this->low_bound || value > this->high_bound)
this->report_set_error (); // Raise exception
}
// operator Type() -- Provide implicit conversion to what ever type this
// class is parameterized over to allow mixed expressions
// Input: None
// Output: Value of object
template <class Type, Type lbound, Type hbound> CoolRange {
inline CoolRange<Type,lbound,hbound>::operator Type() const {return this->data;}
}
// set_compare() -- Set the compare function for the CoolRange class
// Input: Pointer to a compare function
// Output: None
template<class Type, Type lbound, Type hbound>
inline void CoolRange<Type,lbound,hbound>::set_compare (Type##_CoolRange_Compare c) {
this->compare_s = c; // Set compare function
}
#endif // End #ifdef of RANGEH